/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package rhserver;

import Entities.Resource;
import helpers.Debug;
import iohelpers.IOHandler;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

/**
 *
 * @author Bari
 */
public class MainController implements ServletContextListener {

    private static MainController instance;
    private EntityManagerFactory emf;
    private TimerManager tm;
    private IOHandler ioHandler;

    public static synchronized MainController getInstance() {

        if (instance == null) {
            instance = new MainController();
        }
        return instance;
    }

    public EntityManagerFactory getEMF() {
        if (emf==null)
        {
               emf = Persistence.createEntityManagerFactory("RHMServerPU");
               Debug.logMessage("getEMF", "EMF Initialized");
        }
        return emf;
    }

    public EntityManager getEntityManager() {
        if (emf==null)
        {
              emf = Persistence.createEntityManagerFactory("RHMServerPU");
              Debug.logMessage("getEntityManager", "EMF Initialized");
        }
        return emf.createEntityManager();
    }

    public MainController() {
      
        tm = new TimerManager();
        ioHandler = new IOHandler();
    }

    public Date getCurrentDate() {
        Calendar cal = Calendar.getInstance();
        cal.setTimeZone(TimeZone.getTimeZone("GMT"));
        return cal.getTime();
    }

    public void startResourceTimer(long activationLength, long reservationId, Resource resource) {
        tm.startResouceTimer(activationLength, reservationId);
        if (Debug.SEND_SMS) {
            ioHandler.ActivateResource(resource);
        }
    }

    public void smsStopResource(Resource resource) {
        if (Debug.SEND_SMS) {
            ioHandler.DeactivateResource(resource);
        }
    }

    public void smsRestartResource(Resource resource) {
        if (Debug.SEND_SMS) {
            ioHandler.ActivateResource(resource);
        }
        
    }
    
    private void restartActiveReservations()
    {
        tm.restartTimersAfterDB();
    }

    //<editor-fold defaultstate="collapsed" desc="ContextListeners">
    @Override
    public void contextInitialized(ServletContextEvent sce) {
         emf = Persistence.createEntityManagerFactory("RHMServerPU");
        MainController.getInstance().restartActiveReservations();
    }
    

    @Override
    public void contextDestroyed(ServletContextEvent sce) {
        try {
            // close the Entity Manager Factory.
            if (emf!=null)
            {
            emf.close();
            emf=null;
            }
            Debug.logMessage("contextDestroyed", "Context destroyed");
        } catch (Exception e) {
            Debug.logError("contextDestroyed", e);
        }
    }
    //</editor-fold>
}
